package com.interesting.client.UI.home;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.interesting.R;
import com.interesting.base.core.BaseActivity;
import com.interesting.base.http.HttpUtil;
import com.interesting.base.utils.Constant;
import com.interesting.client.UI.commonActivity.BrowserBigPicActivity;
import com.interesting.client.UI.commonActivity.UIUtil;
import com.interesting.client.application.AppHolder;
import com.interesting.client.bean.Message;

/** 审核界面 */
public class ExamineActivity extends BaseActivity implements OnClickListener {

	private TextView content_text;
	private ImageView photo;
	private Button move, funny, nofunny, next;
	private List<Message> examineList = new ArrayList<Message>(); // 待审核数据列表
	private int currentPage = 1, position;
	private List<JSONObject> datalist = new ArrayList<JSONObject>(); // 审核后的数据列表

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home_examine_activity);
		getSupportActionBar().setCustomView(R.layout.text_actionbar_custom_view);
		setTitle(getResources().getString(R.string.examine));

		content_text = (TextView) findViewById(R.id.examine_activity_text); // 审核内容
		photo = (ImageView) findViewById(R.id.examine_activity_photo); // 审核图片
		funny = (Button) findViewById(R.id.examine_activity_funny); // 好笑
		move = (Button) findViewById(R.id.examine_activity_move); // 感动
		nofunny = (Button) findViewById(R.id.examine_activity_nofunny); // 不给力
		next = (Button) findViewById(R.id.examine_activity_next); // 跳过
		funny.setOnClickListener(this);
		move.setOnClickListener(this);
		nofunny.setOnClickListener(this);
		next.setOnClickListener(this);
		photo.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// 图片点击效果
				Intent intent = new Intent(ExamineActivity.this, BrowserBigPicActivity.class);
				intent.putExtra("msg", Constant.IMGPATH + "/" + examineList.get(position).getImgPath());
				startActivity(intent);
			}
		});
		if (savedInstanceState == null)
			new LoadExamineAsync().execute(currentPage + "");
	}

	@Override
	public void onClick(View v) {

		if (!examineList.isEmpty()) {
			String examine = null;
			JSONObject item = new JSONObject();
			switch (v.getId()) {
			case R.id.examine_activity_funny: // 好笑
				examine = "1";
				break;
			case R.id.examine_activity_move:// 感动
				examine = "2";
				break;
			case R.id.examine_activity_nofunny:// 不给力
				examine = "3";
				break;
			case R.id.examine_activity_next:// 下一个
				examine = "0";
				break;
			}
			try {
				item.put("id", examineList.get(position).getId());
				item.put("type", examine);
				datalist.add(item);
			} catch (JSONException e) {
				e.printStackTrace();
			}
			position++;
			showMessage(position);
		}
	}

	private void showMessage(int number) {
		if (number < examineList.size()) {
			Message mesg = examineList.get(number);
			content_text.setText(mesg.getContent());
			if (mesg.getImgPath() != null && !mesg.getImgPath().equals("")) {
				AppHolder.getInsatnce().imageLoader.DisplayImage(Constant.IMGPATH + "/" + mesg.getMinImgPath(), photo,
						R.drawable.ic_launcher);
			}
		} else {
			UIUtil.showWaitDialog(getSupportFragmentManager());
			try {
				examineList.clear();
				JSONArray data = new JSONArray(datalist.toString());
				datalist.clear();

				new UploadExamineAsync().execute(data.toString());
			} catch (JSONException e) {
				e.printStackTrace();
			}
			new LoadExamineAsync().execute(currentPage + "");

		}
	}

	/** 获得审核内容 */
	private class LoadExamineAsync extends AsyncTask<String, Void, String> {

		@Override
		protected String doInBackground(String... params) {
			Map<String, String> request = new HashMap<String, String>();
			request.put("act", "check");
			request.put("userId", AppHolder.getInsatnce().user.getUserId());
			request.put("currentPage",params[0]);
			request.put("rowCountPerPage","5");
			return HttpUtil.post(request);
		}

		@Override
		protected void onPostExecute(String result) {
			JSONObject response = HttpUtil.parseResult(result);
			if (response != null) {

				try {

					Gson gson = new Gson();
					List<Message> dataList = gson.fromJson(response.getString("dataList"),
							new TypeToken<List<Message>>() {
							}.getType());
					if (dataList.size() > 0) {
						for (int i = 0; i < dataList.size(); i++) {
							examineList.add(dataList.get(i));
						}
						position = 0;
						showMessage(position);
					} else {
						UIUtil.showToast(getApplicationContext(), "居然没有审核信息");
					}

				} catch (JsonSyntaxException e) {
					e.printStackTrace();
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			UIUtil.hideDialog(getSupportFragmentManager());
			super.onPostExecute(result);
		}
	}

	/** 上传已审核内容 */
	private class UploadExamineAsync extends AsyncTask<String, Void, JSONObject> {

		@Override
		protected JSONObject doInBackground(String... params) {
			Map<String, String> request = new HashMap<String, String>();
			request.put("act", "check");
			request.put("userId", AppHolder.getInsatnce().user.getUserId());
			request.put("dataList", params[0]);
			currentPage++;
			return HttpUtil.parseResult(HttpUtil.post(request));
		}
	}
}
